﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.IO;


namespace ScenarioOptimizer
{
    class Facade
    {
        private TextBox _tbTeamList;
        private Team _team;
        private string _character;

        public Facade()
        {
            _team = new Team();
        }

        //tested 1/15 - OK
        public void AddTeamMember(TextBox tbTeamList)
        {
            _tbTeamList = tbTeamList;

            DialogResult dr = new DialogResult();
            AddTeamMemberForm add = new AddTeamMemberForm();
            
            dr = add.ShowDialog();

            if (dr == DialogResult.OK)
                _character = add.Character;

            //add to textbox
            if (_tbTeamList.Text.Length > 0)
            {
                _tbTeamList.AppendText(Environment.NewLine);
            }
            _tbTeamList.AppendText(_character);
        }

        //test 1/15 - OK
        public void RemoveTeamMember(string selectedCharacter)
        {
            foreach (string s in _tbTeamList.Lines)
                _team.TeamList.Add(s);

            _team.TeamList.Remove(selectedCharacter);

            _tbTeamList.Text = String.Join(Environment.NewLine, _team.TeamList);
            
            _team.TeamList.Clear();
        }

        //tested 1/15 - OK
        public void CreateOptimalScenarioList(TextBox tbTeamList,TextBox tbOptimizedList)
        {
            List<List<string>> combinations = new List<List<string>>();
            List<string> temp = new List<string>();
            StringBuilder sb = new StringBuilder();

            var result = Combinations.Combination(tbTeamList.Lines, 3);

            foreach (var r in result)
            {
                combinations.Add(r.ToList());
            }

            for (int i = 0; i < combinations.Count; i++)
            {
                for (int j = 0; j < combinations[i].Count; j++)
                {
                    sb.Append(combinations[i][j]).Append(" ");
                }

                temp.Add(sb.ToString());
                sb.Clear();
            }

            tbOptimizedList.Text = String.Join(Environment.NewLine, temp);
        }

        //tested 1/16 - OK
        public void ExportOptimalScenarioList(TextBox tbOptimizedList)
        {
            tbOptimizedList.Text = tbOptimizedList.Text.Replace(' ', ',');
            
            SaveFileDialog sfd = new SaveFileDialog();
            
            sfd.Title = "Save Optimized Team List";
            sfd.Filter = "Comma Separated Value|*.csv";
            sfd.ShowDialog();

            if (sfd.FileName != "")
            {
                StreamWriter sw = new StreamWriter(sfd.FileName);
                sw.Write(tbOptimizedList.Text);

                sw.Close();
            }
            sfd.Dispose();
        }
    }
}
